﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using Devdream.Web.Controllers;
namespace Devdream.Web
{
    public class MvcApplication : System.Web.HttpApplication
    {
        /// <summary>
        /// 定义错误页面跳转
        /// </summary>
        protected void Application_EndRequest()
        {
            if (Context.Response.StatusCode == 400)
            {
                Response.Clear();
                var router = new RouteData();
                router.DataTokens["area"] = "";
                router.Values["controller"] = "Errors";
                router.Values["action"] = "BadRequest";
                IController controller = new ErrorsController();
                controller.Execute(new RequestContext(new HttpContextWrapper(Context), router));
            }
            if (Context.Response.StatusCode == 404)
            {
                Response.Clear();
                var router = new RouteData();
                router.DataTokens["area"] = "";
                router.Values["controller"] = "Errors";
                router.Values["action"] = "NotFound";
                IController controller = new ErrorsController();
                controller.Execute(new RequestContext(new HttpContextWrapper(Context), router));
            }
            if (Context.Response.StatusCode == 500)
            {
                Response.Clear();
                var router = new RouteData();
                router.DataTokens["area"] = "";
                router.Values["controller"] = "Errors";
                router.Values["action"] = "ServerErrors";
                IController controller = new ErrorsController();
                controller.Execute(new RequestContext(new HttpContextWrapper(Context), router));
            }
        }
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            UnityConfig.RegisterComponents();
        }
    }
}
